package com.bw.module_live.anchor.model.repository

import com.bw.module_live.anchor.model.entity.CreateLiveRequestEntity
import com.bw.module_live.anchor.model.entity.SendRedPacketRequestEntity
import com.bw.module_live.api.LiveApi
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import javax.inject.Inject

/**
 * @author ytx
 * @date 2025-10-27 08:17
 * @description
 */
class CreateLiveRepository @Inject constructor(val api: LiveApi) {
    fun createLiveRoom(entity: CreateLiveRequestEntity) = flow {
       val requestBody = Gson().toJson(entity).toRequestBody("application/json; charset=utf-8".toMediaType())
        emit(api.createLiveRoom(requestBody))
    }.flowOn(Dispatchers.IO)

    /**
     * 发红包
     */
    fun sendRedPacket(entity: SendRedPacketRequestEntity) = flow {
        val requestBody = Gson().toJson(entity).toRequestBody("application/json; charset=utf-8".toMediaType())
        emit(api.sendRedPacket(requestBody))
    }.flowOn(Dispatchers.IO)

    /**
     * 上车
     */
    fun sendGoods(goods_id:String,room_id:String) = flow {
        val map = mutableMapOf<String,String>()
        map["room_id"] = room_id
        map["goods_id"] = goods_id
        val requestBody = Gson().toJson(map).toRequestBody("application/json;charset=utf-8".toMediaType())
        emit(api.sendGoods(requestBody))
    }.flowOn(Dispatchers.IO)

    /**
     * 下车
     */
    fun sendGoodsRemove(goods_id: String,room_id: String) = flow {
        val map = mutableMapOf<String,String>()
        map["room_id"] = room_id
        map["goods_id"] = goods_id
        val requestBody = Gson().toJson(map).toRequestBody("application/json;charset=utf-8".toMediaType())
        emit(api.sendGoodsRemove(requestBody))
    }.flowOn(Dispatchers.IO)
}